package controllers;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import play.Logger;

import models.Orders;
import models.Trades;

import com.etaoking.commons.BeanUtilsEx;
import com.etaoking.commons.Contants;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.Order;
import com.taobao.api.domain.Trade;
import com.taobao.api.request.TradeFullinfoGetRequest;
import com.taobao.api.request.TradesBoughtGetRequest;
import com.taobao.api.response.TradeFullinfoGetResponse;
import com.taobao.api.response.TradesBoughtGetResponse;


public class TradeAction extends AdminController{
	
	
	public static void index(){
		Logger.info("访问订单列表!");
		TaobaoClient client=new DefaultTaobaoClient(Contants.url, Contants.appKey, Contants.secret);
		TradesBoughtGetRequest req=new TradesBoughtGetRequest();
		req.setPageNo(new Long(1));
		req.setPageSize(new Long(50));
		req.setFields("seller_nick,buyer_nick,title,type,created,sid,tid,seller_rate,buyer_rate,status,payment,discount_fee,adjust_fee,post_fee,total_fee,pay_time,end_time,modified,consign_time,buyer_obtain_point_fee,point_fee,real_point_fee,received_payment,commission_fee,pic_path,num_iid,num_iid,num,price,cod_fee,cod_status,shipping_type,receiver_name,receiver_state,receiver_city,receiver_district,receiver_address,receiver_zip,receiver_mobile,receiver_phone,orders.title,orders.pic_path,orders.price,orders.num,orders.iid,orders.num_iid,orders.sku_id,orders.refund_status,orders.status,orders.oid,orders.total_fee,orders.payment,orders.discount_fee,orders.adjust_fee,orders.sku_properties_name,orders.item_meal_name,orders.buyer_rate,orders.seller_rate,orders.outer_iid,orders.outer_sku_id,orders.refund_id,orders.seller_type");
		try {
			TradesBoughtGetResponse response = client.execute(req , Contants.UserInfo.current().sessionKey);
			List<Trade> trades = response.getTrades();
			List<String> orderStatus = new ArrayList<String>();
			for (Trade trade : trades) {
				List<Order> orders = trade.getOrders();
				for (Order order : orders) {
					if(!Orders.exist(order.getOid())){
						System.out.println("orderId:"+order.getOid());
						orderStatus.add(order.getOid().toString());
					}
				}
			}
			render(trades,orderStatus);
		} catch (ApiException e) {
			Logger.error("访问淘宝接口出错!",e);
		}
	}
	
	
	/**
	 * 发布商品
	 * 
	 * @param order
	 * @return
	 */
	public static void addOrder(Long tid){
		if(Trades.exist(tid)){
			
		}
		TaobaoClient client=new DefaultTaobaoClient(Contants.url, Contants.appKey, Contants.secret);
		TradeFullinfoGetRequest req=new TradeFullinfoGetRequest();
		req.setFields("seller_nick,buyer_nick,title,type,created,sid,tid,seller_rate,buyer_rate,status,payment,discount_fee,adjust_fee,post_fee,total_fee,pay_time,end_time,modified,consign_time,buyer_obtain_point_fee,point_fee,real_point_fee,received_payment,commission_fee,pic_path,num_iid,num_iid,num,price,cod_fee,cod_status,shipping_type,receiver_name,receiver_state,receiver_city,receiver_district,receiver_address,receiver_zip,receiver_mobile,receiver_phone,orders.title,orders.pic_path,orders.price,orders.num,orders.iid,orders.num_iid,orders.sku_id,orders.refund_status,orders.status,orders.oid,orders.total_fee,orders.payment,orders.discount_fee,orders.adjust_fee,orders.sku_properties_name,orders.item_meal_name,orders.buyer_rate,orders.seller_rate,orders.outer_iid,orders.outer_sku_id,orders.refund_id,orders.seller_type");
		req.setTid(tid);
		try {
			TradeFullinfoGetResponse response = client.execute(req , Contants.UserInfo.current().sessionKey);
			Trade trade = response.getTrade();
			List<Order> orders = trade.getOrders();
			Orders order = new Orders();
			models.Trades myTrade = new models.Trades();
			try {
				BeanUtilsEx.copyProperties(myTrade, trade);
			} catch (InvocationTargetException e1) {
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				e1.printStackTrace();
			}
			myTrade.save();
			for (Order order2 : orders) {
				try {
					BeanUtilsEx.copyProperties(order, order2);
					order.setTrade(myTrade);
					order.save();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		} catch (ApiException e) {
			e.printStackTrace();
		}
		index();
	}
}
